package com.societyGames.precedenceExercises
{
	import com.societyGames.action.ActionQueue;

	public class PrecedenceSimulation
	{
		private var _render:PrecedenceRender;
		private var _expression:INumberExpression;

		public function PrecedenceSimulation(actionQueue:ActionQueue)
		{
			this._render = new PrecedenceRender(this, actionQueue);
			
		}

		public function get render():PrecedenceRender
		{
			return this._render;
		}

		public function initialize(xml:XML):void
		{	
			//values
			if ("x" in xml.values)
			{
				FunctionCallExpression.x = Number(xml.values[0].x[0].toString());
			}
			else
			{
				FunctionCallExpression.x = 0;
			}
			if ("y" in xml.values)
			{
				FunctionCallExpression.y = Number(xml.values[0].y[0].toString());
			}
			else
			{
				FunctionCallExpression.y = 0;
			}
			if ("z" in xml.values)
			{
				FunctionCallExpression.z = Number(xml.values[0].z[0].toString());
			}
			else
			{
				FunctionCallExpression.z = 0;
			}
			
			//Expression
			this._expression = Parser.parse(xml.expression.children()[0]);
			this._render.setExpression(this._expression, FunctionCallExpression.x, FunctionCallExpression.y, FunctionCallExpression.z);
		}

		public function evaluate():Number
		{
			return this._expression.evaluate();
		}
	}
}